use crate::solana::util::get_rarity;
use serde_json::json;
use std::{collections::HashMap, convert::Infallible};

pub async fn get_rarity_handler(
    mint: String,
    formula: HashMap<String, String>,
) -> Result<warp::reply::WithHeader<warp::reply::WithHeader<warp::reply::Json>>, Infallible> {
    let rarity = match get_rarity(
        &mint,
        formula
            .get("formula")
            .unwrap_or(&"all".to_string())
            .parse()
            .unwrap(),
    )
    .await
    {
        Ok(value) => value,
        Err(_) => json!({}),
    };

    let reply = warp::reply::json(&rarity);

    Ok(warp::reply::with_header(
        warp::reply::with_header(
            reply,
            "Access-Control-Allow-Origin",
            "http://localhost:1234",
        ),
        "Access-Control-Allow-Headers",
        "*",
    ))
}
